package com.edeweiss.community.dao.daoImp;

import com.edeweiss.community.dao.ActivityDao;
import com.edeweiss.community.domain.Activity;
import com.edeweiss.community.domain.Admin;
import com.edeweiss.community.web.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.util.Date;
import java.util.List;

public class ActivityDaoImpl implements ActivityDao {
    private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
    @Override
    public int applyConductActivity(Activity a) {
        int i  = 0;
        Object[] params = {a.getAname(),a.getAtime(),a.getAplace(),a.getAintroduce(),a.getCommunity_id(),a.getAstatus(),a.getMoney(),a.getBudget()};
        String sql = "insert into activity (aname,atime,aplace,aintroduce,community_id,astatus,money,budget) values (?,?,?,?,?,?,?,?)";
        try {
            i = qr.update(sql,params);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override//根据参数选择查询的条件
    public List<Activity> findAllActivity(int status) {
        String sql = "select * from activity where astatus = ?";
        List<Activity> activities = null;
        try {
            activities = qr.query(sql,new BeanListHandler<Activity>(Activity.class),status);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return activities;
    }

    @Override
    public int applyJoinActivity(int sid, int aid) {
        int i  = 0;
        String sql = "insert into activity_apply (sid,activity_id,status) values (?,?,?)";
        try {
            i = qr.update(sql,sid,aid,0);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int delActivity(int aid) {
        int i  = 0;
        String sql = "delete from activity where aid = ?";
        try {
            i = qr.update(sql,aid);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int activityApprove(int aid, int result,int cid) {
        int i  = 0;
        String msg = result==1?"同意社团举办活动！":"不同意社团举办活动！";
        Connection conn = JDBCUtils.getConnection();
        String sql = "update activity set result = ?,astatus = ?  where aid = ?";
        String sql1="insert into community_message(cid,message,status,time) value (?,?,?,?)";
        try {
            JDBCUtils.startTransaction(conn);
            qr.update(conn,sql,result,1,aid);
            qr.update(conn,sql1,cid,msg,0,new Date());
            JDBCUtils.commitTransaction(conn);
            i = 1;
        }catch (Exception e) {
            JDBCUtils.rollbackTransaction(conn);
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public List<Activity> indexFindActivity() {
        List<Activity> activities = null;
        String sql = "select * from activity where result = ? and atime > ?";

        try {
            activities = qr.query(sql,new BeanListHandler<Activity>(Activity.class),1,new Date());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return activities;
    }

}
